/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import javax.microedition.lcdui.Graphics;

/**
 *
 * @author kien.tran
 */
public class ButtonItem extends AFormItem {

    public ButtonItem(String id, String title) {
        super(id, title);
    }

    public void paint(int x, int y, Graphics g) {
        paint(x, y, g, false);
    }

    public void paint(int x, int y, Graphics g, boolean validate) {
        if (validate && !isPaint(x, y)) {
            return;
        }

        // pre-calc
        calculateHeight();
        if (titleRows == null) {
            setTitleRows(title);
        }

        // paint background
        int titleW, tempX, tempY, i;
        if (titleRows.length == 1) {
            //titleW = boldFont.stringWidth(title) + titleStyle.getPadding().getLeftAndRight();
            titleW = font.getFontBold().stringWidth(title) + titleStyle.getPadding().getLeftAndRight();
            tempX = x + (titleStyle.getWidth() - titleW) / 2;
        } else {
            titleW = titleStyle.getWidth();
            tempX = x + style.getPadding().getLeft();
        }
        g.setColor(focus == false ? titleStyle.getBgColor() : titleStyle.getSelectedBgColor());
        g.fillRect(tempX, y + style.getPadding().getTop(), titleW, titleStyle.getHeight());
        g.setColor(focus == false ? titleStyle.getBorderColor() : titleStyle.getSelectedBorderColor());
        g.drawRect(tempX, y + style.getPadding().getTop(), titleW - 1, titleStyle.getHeight() - 1);

        // paint title
        if (title.length() > 0) {
            tempY = y + style.getPadding().getTop() + titleStyle.getPadding().getTop();
            for (i = 0; i < titleRows.length; i++) {
//                titleW = boldFont.stringWidth(titleRows[i]);
                titleW = font.getFontBold().stringWidth(titleRows[i]);
                tempX = x + (titleStyle.getWidth() - titleW) / 2;
//                tempY += i * (boldFont.getHeight() + titleStyle.getRowHeight());
                tempY += i * (font.getFontBold().getHeight() + titleStyle.getRowHeight());
                font.getFontBold().drawString(g, titleRows[i], tempX, tempY, Graphics.TOP | Graphics.LEFT);
            }
        }
    }

    protected void calculateHeight() {
        int textH = 0;
        if (titleRows == null) {
            setTitleRows(title);
        }

        if (title.length() > 0) {
            textH += titleRows.length * font.getFontNormal().getHeight();
            textH += (titleRows.length - 1) * titleStyle.getRowHeight();
            textH += titleStyle.getPadding().getTopAndBottom();

            titleStyle.setHeight(textH);
            style.setHeight(textH + style.getPadding().getTopAndBottom());
            this.height = style.getHeight();

        } else {
            titleStyle.setHeight(0);
            style.setHeight(0);
            this.height = 0;
        }
    }

    protected void setTitleRows(String title) {
        if (title.length() > 0) {
            titleStyle.setWidth(width - style.getPadding().getLeftAndRight());
//            titleRows = font.splitWith(title, titleStyle.getWidth() - titleStyle.getPadding().getLeftAndRight());
            titleRows = new String[]{title};
        } else {
            titleRows = new String[]{""};
            titleStyle.setWidth(0);
        }
    }

    public void setParent(ACanvas parent) {
        super.setParent(parent);
        if (parent != null) {
            this.titleStyle = parent.getStyle().getButtonStyle();
        }
    }
}
